package main

import (
	"fmt"
	"zinx/utils"
	"zinx/ziface"
	"zinx/znet"
)

//// 提供一个业务类，继承baseRouter
type pingRouter struct {
	baseRouter znet.BaseRouter
}

/// 在处理conn业务之前调用的函数hook
func (this *pingRouter) PreHandle(request ziface.IRequest) {
	fmt.Println("callback PreHandle Start...")
	_, err := request.GetConn().GetTCPConnection().Write([]byte("ping PreHandle...\n"))
	if err != nil {
		fmt.Println("PreHandle func faild... err = ", err)
	}
}

/// 在处理conn业务时调用的主要处理函数
func (this *pingRouter) Handle(request ziface.IRequest) {
	fmt.Println("callback Handle Start...")
	_, err := request.GetConn().GetTCPConnection().Write([]byte("ping Handle...\n"))
	if err != nil {
		fmt.Println("Handle func faild... err = ", err)
	}
}

/// 在处理conn业务之后调用的函数hook
func (this *pingRouter) PostHandle(request ziface.IRequest) {
	fmt.Println("callback PostHandle Start...")
	_, err := request.GetConn().GetTCPConnection().Write([]byte("ping PostHandle...\n"))
	if err != nil {
		fmt.Println("PostHandle func faild... err = ", err)
	}
}

func main() {
	/// 启动服务器
	s := znet.NewSever()
	utils.GlobalObject.TcpServer = s

	/// 创建一个路由
	router := pingRouter{}
	s.AddRouter(&router)

	s.Serve()
}
